<?php
class core{

	function display($url){
		$router=get('_router')->solve($url);
		var_dump($router);
		$route_params = $router->extract_params($url);
		try{
			$control_class_name=$router->page."_page";
			$control = new $control_class_name();
			$this->_control_call($control,$route_params);
			$response = $this->_get_render()->draw($control, 'xml');
		}catch(page_exception $e){

		}
	}

	protected function _get_render(){
		$render=new _render();
		return $render;
	}

	function _control_real_params($control,$parameters){
		$reflect = new ReflectionMethod($control,"execute");
		$real_params=array();
		foreach ($reflect->getParameters() as $i => $param){
			$pname = $param->getName();
			if ($param->isPassedByReference()){
				/// @todo shall we raise some warning?
			}
			if (array_key_exists($pname, $parameters)){
				$real_params[] = $params[$pname];
			}else if ($param->isDefaultValueAvailable()) {
				$real_params[] = $param->getDefaultValue();
		        }else{
				// missing required parameter: mark an error and exit
				//return new Exception('call to '.$function.' missing parameter nr. '.$i+1);
				trigger_error(sprintf('call to %s missing parameter '.$pname.'. %d', $function, $i+1), E_USER_ERROR);
				return NULL;
			}
		}
		return $real_params;
	}

	function _control_call($control,$parameters){
		$real_params=$this->_control_real_params($control,$parameters);
		call_user_func_array(array($control,'execute'),$real_params);
	}


}